Se escribe código dentro de un script como sigue:
Este código sirve para todo el documento.
La tabla de datos es mi componente.
Las tablas interactivas son datos tabulares con controles de ordenación, filtro y paginación. Pageleght indica el número de filas visibles! que tengo en mi tabla de datos.
Los gráficos estáticos se crean con ggplot. Tiene parámetros que permiten redimensionar el gráfico. Voy a ver el total de alumnos por curso.
Para crear gráficos dinámicos uso plotly en combinación con ggplot.
---
title: "Alumnado UCM"
author: "Andrea Martín-Gallego"
date: "r Sys.Date()"
output:
flexdashboard::flex_dashboard:
theme: journal # cambio el looking
source_code: embed
logo: woman.png
---
Se escribe código dentro de un script como sigue:
```{r setup, include=FALSE}
library(flexdashboard)
library(readr)
library(dplyr)
library(ggplot2)
library(DT)
library(plotly)
df <- read_delim("datos_tratados.csv", delim = ";")
df$CURSO <- factor(df$CURSO)
df$CENTRO <- factor(df$CENTRO)
# Agrupamoslos datos por curso
grouped_data <- df %>%
group_by(CURSO) %>%
summarise(across(where(is.numeric), sum))
# creo el dataframe que calcula totales por género.
df_4_M <- select(grouped_data,CURSO,TOTAL_MUJERES)
df_4_H <- select(grouped_data,CURSO,TOTAL_HOMBRES)
colnames(df_4_M)[2] <-"TOTAL"
colnames(df_4_H)[2] <-"TOTAL"
df_4_M$GÉNERO <- rep('Mujer', times = nrow(df_4_M))
df_4_H$GÉNERO <- rep('Hombre', times = nrow(df_4_H))
df_5 <- bind_rows(df_4_M,df_4_H)
```
Este código sirve para todo el documento.
# Página 1
## Columna 1
### Tabla de datos
```{r}
# Crear una tabla interactiva con DT
tabla_interactiva <- datatable(df, options = list( pageLength = 25))
tabla_interactiva
```
La tabla de datos es mi componente.
Las tablas interactivas son datos tabulares con controles de ordenación, filtro y paginación. Pageleght indica el número de filas visibles! que tengo en mi tabla de datos.
## Columna 2
### Total de alumnos por curso
Los gráficos estáticos se crean con ggplot. Tiene parámetros que permiten redimensionar el gráfico.
Voy a ver el total de alumnos por curso.
1. __Paso 1__
2. __Paso 2__
```{r fig.width=10, fig.height=5}
ggplot(df, aes(x = CURSO, y = TOTAL)) +
geom_col() +
labs(title = "U.C.M.", x = "Curso", y = "Total de alumno/as.",
) +
theme(axis.text.x = element_text(angle = 30, vjust = 1, hjust = 1, size = 8))
```
# Página 2
## columna
### Componente dinámico con ggplot + plotly (no title)
Para crear gráficos dinámicos uso plotly en combinación con ggplot.
```{r}
g1 <-ggplot(df_5, aes(x = CURSO, y = TOTAL, fill = GÉNERO)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Total de Alumnos",
x = "CURSO",
y = "Total") +
scale_fill_manual(values = c("Mujer" = "blue", "Hombre" = "red")) +
theme(axis.text.x = element_text(angle = 30, vjust = 1, hjust = 1, size = 8))
g2 <- ggplotly(g1) #aquí convierto el ggplot en plotly
g2
```
### Componente estático con ggplot
```{R}
df_3 <- filter(df, CENTRO == "DERECHO" | CENTRO == "CIENCIAS_DE_LA_INFORMACION")
df_3$CURSO <- factor(df_3$CURSO)
df_3$CENTRO <- factor(df_3$CENTRO)
df_4_M <- select(df_3,CURSO,CENTRO,TOTAL_MUJERES)
df_4_H <- select(df_3,CURSO,CENTRO,TOTAL_HOMBRES)
colnames(df_4_M)[3] <-"TOTAL"
colnames(df_4_H)[3] <-"TOTAL"
df_4_M$GÉNERO <- rep('Mujer', times = nrow(df_4_M))
df_4_H$GÉNERO <- rep('Hombre', times = nrow(df_4_H))
# Ahora uno las tablas de Hombre y Mujer
df_5 <- bind_rows(df_4_M,df_4_H)
g1 <-ggplot(df_5) +
geom_boxplot(aes(y=TOTAL, x = CENTRO, fill = GÉNERO) ) +
labs(title = "Distribución de las mujeres y hombres que estudian en la UCM",
subtitle = "Gráficos de cajas, (geom_boxplot).",
x = " ",
y = "Número de estudiantes \n ",
caption = "Fuente: El Centro de Inteligencia Institucional, UCM."
) +
theme(axis.text.x = element_text(size = 10),
#arriba(top), izquierda (left), derecha (right)
legend.position = "top",
)
g1
```
# Prueba
## Columna 1
### Componente estático de Fac. Ciencias.
```{R}
#creo mi base de datos
ciencias <- filter(df, CENTRO == "MATEMATICAS" | CENTRO == "BIOLOGICAS" | CENTRO == "QUIMICAS" | CENTRO == "FISICAS")
# elaboro gráfico estático
gciencias1 <- ggplot(ciencias) +
geom_col(aes(x = CURSO, y = TOTAL, fill = CENTRO), position = "fill") + # position fill es para que llene las barras hasta arriba
labs(title = "Gráfico evolución proporciones Fac. Ciencias",
subtitle = "Gráfico con fill = CENTRO y position = fill",
x = "Curso",
y = "Total de alumno/as.",
caption = "Datos tomados del Centro de Inteligencia Institucional, U.C.M."
) +
theme(axis.text.x = element_text(angle = 30, vjust = 1, hjust = 1, size = 8),
# arriba(top), izquierda (left), derecha (right), abajo (bottom)
legend.position = "right"
) +
theme_minimal() +
scale_fill_manual(
values = c(
"MATEMATICAS" = "indianred3",
"BIOLOGICAS" = "pink",
"QUIMICAS" = "indianred4",
"FISICAS" = "indianred2"
)
)
gciencias1
```
## COlumna 2
### Componente dinámico de Fac. Ciencias.
```{R}
#creo mi base de datos
ciencias <- filter(df, CENTRO == "MATEMATICAS" | CENTRO == "BIOLOGICAS" | CENTRO == "QUIMICAS" | CENTRO == "FISICAS")
# elaboro gráfico estático
gciencias2 <- ggplot(ciencias) +
geom_col(aes(x = CURSO, y = TOTAL, fill = CENTRO), position = "fill") + # position fill es para que llene las barras hasta arriba
labs(title = "Gráfico evolución proporciones Fac. Ciencias",
subtitle = "Gráfico con fill = CENTRO y position = fill",
x = "Curso",
y = "Total de alumno/as.",
caption = "Datos tomados del Centro de Inteligencia Institucional, U.C.M."
) +
theme(axis.text.x = element_text(angle = 30, vjust = 1, hjust = 1, size = 8),
# arriba(top), izquierda (left), derecha (right), abajo (bottom)
legend.position = "right"
) +
theme_minimal() +
scale_fill_manual(
values = c(
"MATEMATICAS" = "indianred3",
"BIOLOGICAS" = "pink",
"QUIMICAS" = "indianred4",
"FISICAS" = "indianred2"
)
)
#lo dinamizo
gciencias2<- ggplotly(gciencias2)
gciencias2
```